/**
* Jquery上传操作组件
*
* 本程序主要作用用来在弹出窗口中形成上传文件功能
* 
* @category   Javascript
* @package    Javascript
* @copyright  Copyright (c) ink工作室 (http://www.inkphp.com)
* @author     张旭刚
* @version    v1.0 beta
*/
$(document).ready(function(){
    $('#fileurl').click(function(){
        setUrlTextNull($(this));
    });
    $('#fileurl').blur(function(){
        setUrlText($(this));
    });
    $('#uploadCancel').click(function(){
        $.win.close(); 
    });
    $('#tab_1').click(function(){
        showTab($(this)); 
    });
    $('#tab_2').click(function(){
        showTab($(this)); 
    });
    $('#tab_3').click(function(){
        showTab($(this)); 
    });
    
    var settings = {
        flash_url : "/core/static/Public/js/swfupload.swf",
        upload_url: "/uploadfile",
        post_params: {"PHPSESSID" : "<?php echo session_id(); ?>"},
        file_size_limit : $('#file_size_limit').attr('value'),
        file_types : $('#file_types').attr('value'),
        file_types_description : $('#file_types_description').attr('value'),
        file_upload_limit : $('#file_upload_limit').attr('value'),
        file_queue_limit : $('#file_upload_limit').attr('value'),
        custom_settings : {
                progressTarget : "fsUploadProgress",
                cancelButtonId : "btnCancel"
        },
        debug: false,
        button_image_url: "/core/static/Public/css/images/uploadBtn.png",
        button_width: "71",
        button_height: "25",
        button_placeholder_id: "spanButtonPlaceHolder",
//      button_text: '<span class="theFont">Hello</span>',
//      button_text_style: ".theFont { font-size: 16; }",
        button_text_left_padding: 12,
        button_text_top_padding: 3,
        file_queued_handler : fileQueued,
        file_queue_error_handler : fileQueueError,
        upload_start_handler : uploadStart,
        upload_progress_handler : uploadProgress,
        upload_error_handler : uploadError,
        upload_success_handler : uploadSuccess,
        upload_complete_handler : eval($('#upload_complete_handler').attr("value")),
        queue_complete_handler : queueComplete	// Queue plugin event
    };
    var swfu = new SWFUpload(settings);
    $('#startUpload').click(function(){
        var a = swfu.startUpload();
    });
    
    $('#fsUploadProgress').change(function(){
        $.win.setCenter('iframe');
    });
});

var data = new Array();
var key = 0;
function upload_success(d){
    data[key] = d;
    key++;
    return true;
}

function logo_complete(){
    $('#'+returnId).attr('value', data[0]);
    data = null;
    $.win.close();
}

function pageIndex_complete(){
    var html = '';
    var len = data.length;
    var num = parseInt($('#menuNum').attr('value'));
    for(var i = 0; i < len; i++){
        var k = i+num;
        html += '<h1>';
        html += '   <span>图片地址：</span>';
        html += '   <em><input name="value['+k+'][photourl]" type="text" value="'+data[i]+'"></em>';
        html += '   <span>描述：</span>';
        html += '   <em><input name="value['+k+'][description]" type="text" value="'+$.str.getBaseName(data[i])+'"></em>';
        html += '   <span id="delPhoto_'+(k+1)+'" class="delPhoto">删除</span>';
        html += '   <div class="clear"></div>';
        html += '</h1>';
        $('#newPageIndex').append(html);
        html = '';
        $('#delPhoto_'+(k+1)).click(function(){
            var obj = $(this).parent();
            obj.remove();
        });
    }
    data = null;
    $('#newPageIndex').append(html);
    $.win.close();
}


var dispTab = '1';
function showTab(obj){
    if(dispTab == null){
        dispTab = '1';
    }
    var id = obj.attr('id').replace('tab_','');
    if(id != dispTab){
        $('#file_'+id).show();
        $('#tab_'+id).attr('class','selected');
        $('#file_'+dispTab).hide();
        $('#tab_'+dispTab).attr('class','');
        dispTab = id;
        $.win.setCenter('iframe');
    }
}

function setUrlTextNull(obj){
    var text = obj.attr('value');
    if(text == '请您输入文件的绝对地址'){
        obj.attr('value','');
    }
}

function setUrlText(obj){
    var text = obj.attr('value');
    if(text == ''){
        obj.attr('value','请您输入文件的绝对地址');
    }
}